import { Directive, Host, HostListener, Self } from '@angular/core';
import { Calendar } from 'primeng/primeng';

@Directive({
    selector: '[qkUtc]'
})
export class QkUtcDirective {

    constructor(@Host() @Self() private calendar: Calendar) { }

    @HostListener('onSelect', ['$event']) onSelect() {
        this.toUtc();
    }

    @HostListener('onInput', ['$event']) onInput() {
        this.toUtc();
    }

    private toUtc() {
        let d = this.calendar.value;
        this.calendar.value = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(),
            d.getHours(), d.getMinutes(), d.getSeconds()));
        this.calendar.updateModel(this.calendar.value);
    }
}